package com.witframework.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;



public class MD5PasswordHasher{

	private MD5PasswordHasher(){}

	public static String hashPassword(String pPassword){
		return encodeDigestHash(md5Digest(pPassword));
	}

	/**
	 * Returns hash bytes in the hexadecimal.
	 */
	private static String encodeDigestHash(byte[] bytes)
	{
		if (bytes == null){
			throw new IllegalArgumentException("byte array must not be null");
		}
		StringBuffer hex = new StringBuffer(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++)
		{
			hex.append(Character.forDigit((bytes[i] & 0XF0) >> 4, 16));
			hex.append(Character.forDigit((bytes[i] & 0X0F), 16));
		}
		return hex.toString();
	}

	private static byte[] md5Digest(String pPassword)
	{
		if (pPassword == null) {
			throw new NullPointerException("input null text for hashing");
		}
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(pPassword.getBytes());
			return md.digest();
		}
		catch (NoSuchAlgorithmException e){
			throw new RuntimeException("Cannot find MD5 algorithm");
		}
	}
	/** used for command lin */
	public static void main(String[] args){

	    String md5hashed =MD5PasswordHasher.encodeDigestHash("4028c6001c0cf8e8011c0d12562a0018".getBytes());
	    System.out.println(md5hashed);
	    
	}
}
